<template>
<el-dialog :append-to-body="true" :close-on-click-modal="false" class="add-dialog add-dialog-project" :title="addForm.title" :visible.sync="addForm.show" width="470px">
    <el-form ref="ruleForm" :model="addForm.form" :rules="rules" label-width="100px">
        <el-form-item label="项目名称" prop="projectName">
            <el-input clearable placeholder="请输入项目名称" v-model="addForm.form.projectName"></el-input>
        </el-form-item>
        <el-form-item label="项目代码" prop="projectCode">
            <el-input :maxlength="20" clearable placeholder="请输入项目代码" v-model="addForm.form.projectCode"></el-input>
        </el-form-item>
        <el-form-item label="项目描述">
            <el-input :maxlength="100" type="textarea" :rows="5" clearable placeholder="请输入项目描述" v-model="addForm.form.projectDesc"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="addForm.show=false">取 消</el-button>
      <el-button type="primary" @click="handleSave">确 定</el-button>
    </span>
</el-dialog>
</template>

<script>
export default {
    name: 'AddDialog',
    props: {
        addForm: {
            type: Object,
            required: true
        }
    },
    data () {
        var validateNameLength = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请输入项目名称'))
            } else if (value !== '') {
                var result = 0
                for (let i = 0; i < value.length; i++) {
                    result += 1
                    // 检测是否是中文，若是中文则占两个字符
                    if (value.charCodeAt(i) > 256) {
                        result += 1
                    }
                }
                if (result > 30 || result < 3) {
                    callback(new Error('长度在3-30个字符,只包含中文、字母、下划线和数字'))
                } else {
                    callback()
                }
            }
        }
        return {
            rules: {
                projectName: [
                    {
                        required: true,
                        validator: validateNameLength,
                        trigger: 'blur'
                    }
                ],
                projectCode: [
                    {
                        required: true,
                        message: '请输入项目代码',
                        trigger: 'blur'
                    },
                    { pattern: /^[a-zA-Z][a-zA-Z0-9_]*$/, message: '不超过20个字符并以字母开头，只包含字母、下划线和数字' }
                ]
            }
        }
    },
    methods: {
        handleSave () {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.$emit('save')
                } else {
                    return false
                }
            })
        },
        resetForm () {
            this.$refs['ruleForm'].resetFields()
        }
    }
}
</script>

<style lang="less">
.add-dialog-project {
    .el-input {
        width: 320px !important;
    }
    .el-textarea__inner {
        width: 320px !important;
    }
}
</style>
